<?php
header('content-type:text/html;charset=utf-8');

class Home_Controller extends Base_Controller {

	/*
	|--------------------------------------------------------------------------
	| The Default Controller
	|--------------------------------------------------------------------------
	|
	| Instead of using RESTful routes and anonymous functions, you might wish
	| to use controllers to organize your application API. You'll love them.
	|
	| This controller responds to URIs beginning with "home", and it also
	| serves as the default controller for the application, meaning it
	| handles requests to the root of the application.
	|
	| You can respond to GET requests to "/home/profile" like so:
	|
	|		public function action_profile()
	|		{
	|			return "This is your profile!";
	|		}
	|
	| Any extra segments are passed to the method as parameters:
	|
	|		public function action_profile($id)
	|		{
	|			return "This is the profile for user {$id}.";
	|		}
	|
	*/


    // public function action_index()
    // {     
    //     return View::make('home.index');
    // }


    public function action_index()
    {
        $username = Session::get('username');
        $id = (isset($_GET['id'])) ? $_GET['id'] : 1;       
        $kinds = DB::query("select * from article_kind order by id asc");
        $titles = DB::query("select * from article_content where kid=$id order by id asc");
        return View::make('home.index',array(
            'kinds'=>$kinds,
            'titles'=>$titles,
            'username'=>$username,
            ));
    }



    public function action_showarticle()
    {
        $id = $_GET['id'];
        $kid = $_GET['kid'];
        $article=DB::table("article_content")
                     ->where('id','=',$id)
                     ->where('kid','=',$kid)
                     ->first();
         $res3 = DB::table('article_content')
                     ->where('id','<',$id)
                     ->where('kid','=',$kid)
                     ->order_by('id','desc')
                     ->first();
         $res4 = DB::table('article_content')
                         ->where('id','>',$id)
                         ->where('kid','=',$kid)
                         ->order_by('id','asc')
                         ->first();
        $message = DB::query("select * from message where kid=$id and flag=1");
        $username = Session::get('username');
         return View::make('home.article',array(
            'article'=>$article,
            'message'=>$message,
            'res3'=>$res3,
            'res4'=>$res4,
            'username'=>$username,
            ));
    }



    public function action_domessage()
    {
        $user = Session::get('username');
        if($user){
            $username = $_POST['username'];
            $message = $_POST['content'];
            $title = $_POST['title'];
            $id = $_POST['id'];
            $kid = $_POST['kid'];
            $time = time()+(8*3600);
            $flag = 0;
            $ms = DB::query("insert into message (username,kid,time,message,title,flag) values('$username',$id,$time,'$message','$title',$flag)");
            if($ms){        
                return View::make('jump/error',array('msg'=>'等待管理员审核.....','time'=>3));    
            }        
        }else{
            return View::make('jump/error2',array('msg'=>'登入后留言','time'=>3,'url'=>'/pass/login'));
        }
    }




}


